package net.fei.scan.quick.ui

import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.text.InputType
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import net.fei.scan.quick.R
import net.fei.scan.quick.databinding.ActivityAddBinding
import net.fei.scan.quick.db.DbUtils
import net.fei.scan.quick.entity.History


class AddActivity : AppCompatActivity() {
    private lateinit var bin: ActivityAddBinding
    private var index = 0
    private var data = ""
    private var history1 = History()
    private var pass = false
    private var encryptionMethod = "WPA"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bin = ActivityAddBinding.inflate(layoutInflater)
        setContentView(bin.root)
        initUi()
    }


    private fun initUi() {
        data = intent.getStringExtra("history").toString()
        if (data!=null&& data.isNotEmpty()&&!data.equals("null")) {
            index = intent.getIntExtra("index", 0)
            val history = Gson().fromJson(data, History::class.java)
            history1.title = history.title
            history1.image = history.image
            history1.content = bin.urlEdit.text.toString()
            bin.apply {
                img.setBackgroundResource(history.image)
                tetx.text = history.title
            }
            when (index) {
                0 -> {
                    bin.apply {
                        urlEdit.visibility = View.VISIBLE
                    }

                }
                1 -> {
                    bin.apply {
                        urlEdit.setText("http//:")
                        urlEdit.visibility = View.VISIBLE
                        submit.setOnClickListener {
                            jumpToTransferData(urlEdit.text.toString())
                        }
                    }
                }
                2 -> {
                    bin.tetxEdit.visibility = View.VISIBLE
                    bin.submit.setOnClickListener {
                        jumpToTransferData(bin.tetxEdit.text.toString())
                    }
                }
                3 -> {
                    bin.lxr1.visibility = View.VISIBLE
                    bin.lxr2.visibility = View.VISIBLE
                    bin.lxr3.visibility = View.VISIBLE
                    bin.lxr4.visibility = View.VISIBLE
                    bin.lxr5.visibility = View.VISIBLE

                    bin.lxr5.hint=getString(R.string.dflhfkh)

                    bin.submit.setOnClickListener {
                        val text1 = bin.lxr1.text.toString()
                        val text2 = bin.lxr2.text.toString()
                        val text3 = bin.lxr3.text.toString()
                        val text4 = bin.lxr4.text.toString()
                        val text5 = bin.lxr5.text.toString()
                        val content = String.format(
                            "BEGIN:VCARD\n" + "VERSION:3.0\n" + "N:%s\n" + "ORG:%s\n" + "ADR:%s\n" + "TEL:%s\n" + "NOTE:%s\n" + "END:VCARD",
                            text1,
                            text2,
                            text3,
                            text4,
                            text5
                        )
                        jumpToTransferData(content)
                    }
                }
                4 -> {
                    bin.apply {
                        lxr3.visibility = View.VISIBLE
                        lxr3.hint = getString(R.string.dhkjshf)
                        lxr4.hint = getString(R.string.sadfgasklg)
                        lxr4.inputType=InputType.TYPE_CLASS_TEXT
                        lxr5.hint = getString(R.string.dfsihgs)
                        lxr4.visibility = View.VISIBLE
                        lxr5.visibility = View.VISIBLE

                        bin.submit.setOnClickListener {
                            val format = String.format(
                                "mailto:%s?subject=%s&body=%s", lxr3.text, lxr4.text, lxr5.text
                            )
                            jumpToTransferData(format)
                        }
                    }
                }
                5 -> {
                    bin.lxr4.visibility = View.VISIBLE
                    bin.lxr5.visibility = View.VISIBLE

                    bin.submit.setOnClickListener {
                        val format = String.format("sms:%s?body=%s", bin.lxr4.text, bin.lxr5.text)
                        jumpToTransferData(format)
                    }

                }
                6 -> {
                    bin.lxr1.visibility = View.VISIBLE
                    bin.lxr2.visibility = View.VISIBLE
                    bin.lxr3.visibility = View.VISIBLE

                    bin.lxr1.hint = getString(R.string.dflshf)
                    bin.lxr2.hint = getString(R.string.dsaukfhgaso)
                    bin.lxr3.hint = getString(R.string.asdiufgsa)


                    bin.submit.setOnClickListener {
                        val format = String.format(
                            "geo:%s,%s?q=%s",
                            bin.lxr1.text to Double,
                            bin.lxr2.text to Double,
                            bin.lxr3.text
                        )
                        jumpToTransferData(format)
                    }
                }
                7 -> {
                    bin.lxr4.visibility = View.VISIBLE
                    bin.lxr4.hint = getString(R.string.dskahnsl);

                    bin.submit.setOnClickListener {
                        val s = "tel:${bin.lxr4.text}"
                        jumpToTransferData(s)
                    }
                }
                8 -> {
                    bin.lxr1.visibility = View.VISIBLE
                    bin.lxr2.visibility = View.VISIBLE
                    bin.spinner.visibility = View.VISIBLE

                    bin.visibility.visibility = View.VISIBLE

                    bin.lxr1.hint = getString(R.string.dkjlb)
                    bin.lxr2.hint = getString(R.string.dsanblsa)

                    ArrayAdapter.createFromResource(
                        this, R.array.planets_array, android.R.layout.simple_spinner_item
                    ).also { adapter ->
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                        bin.spinner.adapter = adapter
                    }

                    bin.spinner.onItemSelectedListener =
                        object : AdapterView.OnItemSelectedListener {
                            override fun onItemSelected(
                                parent: AdapterView<*>, view: View, position: Int, id: Long
                            ) {
                                val item = parent.getItemAtPosition(position).toString()
                                encryptionMethod = item
                            }

                            override fun onNothingSelected(parent: AdapterView<*>) {

                            }
                        }
                    bin.visibility.setOnCheckedChangeListener { _, b ->
                        pass = b
                    }
                    bin.submit.setOnClickListener {
                        val s = String.format(
                            "WIFI:S:%s;T:%s;P:%s;H:%s;;",
                            bin.lxr1.text,
                            encryptionMethod,
                            bin.lxr2.text,
                            pass.toString()
                        )

                        jumpToTransferData(s)
                    }
                }
            }
        }
    }

    private fun jumpToTransferData(content: String) {
        val intent = Intent(this@AddActivity, ContentActivity::class.java)
        intent.putExtra("content", content)
        intent.putExtra("data", data)
        startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        if (index != 0) return
        Log.d("TAG", "焦点")
        Handler().postDelayed({
            val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            if (clipboardManager.hasPrimaryClip()) {
                val clipData = clipboardManager.primaryClip
                if (clipData!!.itemCount > 0) {
                    val text = clipData!!.getItemAt(0).text
                    val clipboardText = text.toString()
                    history1.content = clipboardText
                    bin.submit.setOnClickListener {

                        val text1 = bin.urlEdit.text
                        if (text1 != null) {
                            val intent = Intent(this@AddActivity, ContentActivity::class.java)
                            intent.putExtra("content", text1.toString())
                            intent.putExtra("data", data)
                            startActivity(intent)
                        }

                    }
                    Log.d("TAG", "$clipboardText")
                    bin.urlEdit.setText(clipboardText)
                }
            }
        }, 100)
    }
}